package org.apache.jmeter.gui.rcp;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.util.JOrphanUtils;

public class RcpGUIFactory {
	private static Properties iconProperties = null;

	/** A Map from String to ImageIcon of registered icons. */
	private static final Map ICON_MAP = new HashMap();

	/** A Map from String to ImageIcon of registered icons. */
	private static final Map DISABLED_ICON_MAP = new HashMap();
	
	static {
		// Load Icon Properties file
		final String defaultIconProp = "org/apache/jmeter/images/rcp_icon.properties"; //$NON-NLS-1$
		iconProperties = JMeterUtils.loadProperties(defaultIconProp);
		String iconName1 = null;
		String iconName2 = null;
		
		String[][] icons = getIconMappings();

		for (int i = 0; i < icons.length; i++) {
			iconName1 = icons[i][1].trim();
			
			if (iconName1 != null) {
				registerIcon(icons[i][0], iconName1);

				if (icons[i].length > 2 && icons[i][2] != null) {
					iconName2 = icons[i][2].trim();
					if (iconName2 == null) {
//						log.info("Can't find disabled icon for " + icons[i][0] + " - " + icons[i][2]);
					} else {
						registerDisabledIcon(icons[i][0], iconName2);
					}
				}
			}
		}	
	}

	public static String getIcon(Class elementClass, boolean enabled) {
		return getIcon(elementClass.getName(), enabled);
	}

	public static String getIcon(String elementClassName, boolean enabled) {
		String iconName = (String) (enabled ? ICON_MAP.get(elementClassName) : DISABLED_ICON_MAP.get(elementClassName));
		return iconName;
	}
	/**
	 * Register an icon so that it can later be retrieved via
	 * {@link #getIcon(Class)}. The key should match the fully-qualified class
	 * name for the class used as the parameter when retrieving the icon.
	 * 
	 * @param key
	 *            the name which can be used to retrieve this icon later
	 * @param icon
	 *            the icon to store
	 */
	public static void registerIcon(String key, String iconName) {
		ICON_MAP.put(key, iconName);
	}

	/**
	 * Register an icon so that it can later be retrieved via
	 * {@link #getIcon(Class)}. The key should match the fully-qualified class
	 * name for the class used as the parameter when retrieving the icon.
	 * 
	 * @param key
	 *            the name which can be used to retrieve this icon later
	 * @param icon
	 *            the icon to store
	 */
	public static void registerDisabledIcon(String key, String iconName) {
		DISABLED_ICON_MAP.put(key, iconName);
	}

	public static String[][] getIconMappings() {
		String[][] iconlist = new String[iconProperties.size()][3];
		Enumeration pe = iconProperties.keys();
		int i = 0;
		while (pe.hasMoreElements()) {
			String key = (String) pe.nextElement();
			String icons[] = JOrphanUtils.split(iconProperties.getProperty(key), " ");//$NON-NLS-1$
			iconlist[i][0] = key;
			iconlist[i][1] = icons[0];
			if (icons.length > 1) {
			    iconlist[i][2] = icons[1];
			}
			i++;
		}
		return iconlist;
	}

}
